using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using QLVMBAYDomain;
namespace QLVMBAYDatacess
{
    public class TuyenBayDA : DataAccessBase
    {
		#region Constructors
		
		public TuyenBayDA() : base()
        {
			_selectSql = "Select * From TUYENBAY";
			_insertSql = "Insert Into TUYENBAY(MATB,MASBDI,MASBDEN,TENTB) Values (@MATB,@MASBDI,@MASBDEN,@TENTB)";
        	_updateSql = "Update TUYENBAY Set MASBDI=@MASBDI,MASBDEN=@MASBDEN,TENTB=@TENTB Where 1=1 And MATB=@MATB";
        	_deleteSql = "Delete From TUYENBAY Where 1=1 And MATB=@MATB";
        	_primarSql = "Select * From TUYENBAY Where 1=1 And MATB=@MATB";
        }
		
		protected static TuyenBayDA _inst = null;
		public static TuyenBayDA Inst
		{
			get
			{
				if(_inst == null)
					_inst = new TuyenBayDA();
				return _inst;
			}
		}
		
		#endregion
		
        #region Get Methods

		private TuyenBay GetCurrent(IDataReader reader, bool isDeep)
		{
			TuyenBay ret = new TuyenBay();
			ret.MaTuyenBay = (string)reader["MATB"];
			ret.TenTuyenBay = (string)reader["TENTB"];
			
			ret.ItemSanBayDi = new SanBay();
			if(isDeep)
			{
                ret.ItemSanBayDi = SanBayDA.Inst.GetByPrimaryKey(isDeep, (string)reader["MASBDI"]);
			}
			else
                ret.ItemSanBayDi.MaSanBay = (string)reader["MASBDI"];
			
			ret.ItemSanBayDen = new SanBay();
			if(isDeep)
			{
                ret.ItemSanBayDen = SanBayDA.Inst.GetByPrimaryKey(isDeep, (string)reader["MASBDEN"]);
			}
			else
                ret.ItemSanBayDen.MaSanBay = (string)reader["MASBDEN"];
			
			
			return ret;
		}

		public List<TuyenBay> GetAll(bool isDeep)
		{
			DbCommand comm = GetCommand(_selectSql);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<TuyenBay> ret = new List<TuyenBay>();
					while(dr.Read())
					{
						ret.Add(GetCurrent(dr, isDeep));
					}
					return ret;
                }
            }
		}
		
		public TuyenBay GetByPrimaryKey(bool isDeep, params object[] args)
		{
			DbCommand cmd = GetCommand(_primarSql);
			if(args.Length < 1)
			{
				throw new Exception();
			}
            SetParam(cmd, "@MATB", args[0]);

            using (Connection)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
					if(dr.Read())
					{
						return GetCurrent(dr, isDeep);
					}
					return null;
                }
            }
		}
		
		public bool Insert(TuyenBay entity)
		{
			DbCommand cmd = GetCommand(_insertSql);
            SetParam(cmd, "@MATB", entity.MaTuyenBay);
            SetParam(cmd, "@TENTB", entity.TenTuyenBay);

            SetParam(cmd, "@MASBDI", entity.ItemSanBayDi.MaSanBay);
            SetParam(cmd, "@MASBDEN", entity.ItemSanBayDen.MaSanBay);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Update(TuyenBay entity)
		{
			DbCommand cmd = GetCommand(_updateSql);
            SetParam(cmd, "@MATB", entity.MaTuyenBay);
            SetParam(cmd, "@TENTB", entity.TenTuyenBay);

			SetParam(cmd, "@MASBDI", entity.ItemSanBayDi.MaSanBay);
			SetParam(cmd, "@MASBDEN", entity.ItemSanBayDen.MaSanBay);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Delete(TuyenBay entity)
		{
			DbCommand cmd = GetCommand(_deleteSql);
            SetParam(cmd, "@MATB", entity.MaTuyenBay);

            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		#endregion
    }
}


